var sourceData
var numPages = ''
var oldPage = 0
var videoProbs = []
var timer
var interval = 60 * 1000
var pageType=''
mui.init();
mui.plusReady(function () {
	baseSetup();
})

function getVideoProblems(){
	var parr = sourceData['subID']
	var pdict = {'parr':parr}
	//console.log(JSON.stringify(pdict))
	var paramDict = {'method':'web.INMTeaComm:FindVideoProblems','params':JSON.stringify(pdict),'type':'RecQuery'}
	mui.DHCWebService(paramDict, function(result) {
		//console.log(JSON.stringify(result))
		if (result['msgcode'] != '1') {
			mui.toast(result.msg)
			return
		}
		videoProbs = result.data
		if (videoProbs.length > 0) {
			timer = setTimeout(function(){
				gotoProblemsPage()
			},interval)
		}else if((oldPage+'') == numPages+''){
			gotoNextStep(1)
		}
	}, function(errorStr) {
		mui.toast(errorStr)
	});
}

function gotoProblemsPage(){
	var webview = plus.webview.create('../../TeachPages/videoProblems.html', 'videoProblems', {
		background: '#fff',
		statusbar: {
			background: '#fff'
		}
	},{sourceData:sourceData,queDict:videoProbs[0]});
	webview.show('fade-in');
	webview.addEventListener('close',function(){
		videoProbs.splice(0,1)
		if (videoProbs.length > 0) {
			timer = setTimeout(function(){
				gotoProblemsPage()
			},interval)
		}else if((oldPage+'') == numPages+''){
			gotoNextStep(1)
		}
	})
}

function baseSetup(){
	var self = plus.webview.currentWebview()
	sourceData = self.sourceData
	pageType = self.pageType||''
	if (sourceData['videoCurrentTime'] != '') {
		oldPage = parseInt(sourceData['videoCurrentTime'])
	}
	if (pageType != '' || !localStorage['trainSubID']) {
		mui('#comment')[0].style.top = 'calc(100% - 40px)'
	}else{
		mui('#comment')[0].style.display = 'none'
	}
	
	if (pageType == 'train') { //需要做题和保存进度
		getVideoProblems() //获取的是所有的题目
	}
}

function gotoNextStep(step,currPage){
	if (sourceData['DrillStep'] != "" && sourceData['DrillStep'] != "0") {
		return
	}
	if (step == 1) {
		currPage = numPages
	}
	var parr = sourceData['subID']+'^'+step+'^'+numPages+'^'+currPage
	var pdict = {'parr':parr}
	//console.log(JSON.stringify(pdict))
	var paramDict = {'method':'web.INMTeaComm:SaveDrillStep','params':JSON.stringify(pdict),'type':'Method'}
	mui.DHCWebService(paramDict, function(result) {
		//console.log(JSON.stringify(result))
		if (result['msgcode'] != '1') {
			mui.toast(result.msg)
			return
		}
		if (numPages == currPage && videoProbs.length == 0) {
			gotoNextStep(1)
		}
	}, function(errorStr) {
		mui.toast(errorStr)
	});
}

function showPageInfo(page){
	if (pageType != 'train') { //不是
		return
	}
	if (numPages == '') {
		var text = document.getElementById("numPages").innerText
		text = text.replace("/",'')
		numPages = text.replace(" ",'')
	}
	if (oldPage > parseInt(page)) {
		return
	}
	oldPage = currPage
	gotoNextStep(0,page)
}
window.addEventListener('message', function(event) {
	var msgDcit = event.data
	var flag = msgDcit['flag']
	if (flag == '0') {
		$('#comment')[0].style.top = 'calc(100% - 40px)'
	}else{
		$('#comment')[0].style.top = '50%'
	}
}, false);